package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.LimitHelper;
import eteam.aps.model.entity.other.ProcessQuality;
import eteam.aps.model.entity.other.ScanProcess;
import eteam.aps.model.vo.moco_rders.MocoRdersDisplayVo;
import eteam.aps.model.vo.scan.ScanProcessShow;
import eteam.aps.service.ProcessQualityService;
import eteam.aps.zn.ZNApiConstant;
import eteam.aps.zn.ZNApiLimit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工序质检
 *
 * @author LiuLei
 */
@Api(tags = "工序质检")
@RestController
@RequestMapping("/pqc")
public class ProcessQualityController {

    @Autowired
    private ProcessQualityService processQualityService;

    @PostMapping("sinfo")
    @ApiOperation("扫描基本信息")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<?>> scanInfo(@RequestBody ScanProcess scanProcess) throws Exception {
        try {
            LimitHelper.limit(scanProcess.getScan(), ZNApiLimit.ARRIVAL_SAVE_TIME, ZNApiLimit.ARRIVAL_SAVE_UNIT);
            if (Helper.isEmpty(scanProcess.getScan()))
                throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描内容不能为空！！");
            ScanProcessShow<?> show = processQualityService.scanInfo(scanProcess);
            return R.success(show);
        } finally {
            LimitHelper.removeLimit(scanProcess.getScan());
        }
    }

    @PostMapping("submit")
    @ApiOperation("报工")
    @ApiImplicitParam(name = "ScanProcess", value = "扫描信息")
    public R<ScanProcessShow<MocoRdersDisplayVo>> submit(@RequestBody ProcessQuality processQuality) throws Exception {
        try {
            LimitHelper.limit(processQuality.getScan(), ZNApiLimit.ARRIVAL_SAVE_TIME, ZNApiLimit.ARRIVAL_SAVE_UNIT);
            if (!ZNApiConstant.SUBMIT.contains(processQuality.getScan()))
                throw new BusinessException(HttpStatus.BAD_REQUEST, "报工只能是00,01！！");
            if (Helper.isEmpty(processQuality.getBarCode()))
                throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描条码不能为空！！");
            if (Helper.isEmpty(processQuality.getWpCode()))
                throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描工序不能为空！！");
            if (Helper.isEmpty(processQuality.getUserCode()))
                throw new BusinessException(HttpStatus.BAD_REQUEST, "扫描内容不能为空！！");
            if (processQuality.getScan().equals("01") && Helper.isEmpty(processQuality.getRemark()))
                throw new BusinessException(HttpStatus.BAD_REQUEST, "不良现象不能为空！！");
            ScanProcessShow<MocoRdersDisplayVo> submit = processQualityService.submit(processQuality);
            return R.success(submit);
        } finally {
            LimitHelper.removeLimit(processQuality.getScan());
        }
    }


}
